﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using NRails;
using NRails.Spark;
using Spark;
using Spark.FileSystem;
using Nemerle.Contrib;
using Spark.Compiler;
using Spark.Parser.Code;

namespace NRails.Tests.Spark
{
    /// <summary>
    /// Description of SyntaxExtensionTests.
    /// </summary>
    [TestFixture]
    public class SyntaxExtensionTests
    {
        MakeEngine() : SparkViewEngine
        {
            SparkViewEngine() with
                         {
                             ViewFolder = EmbeddedViewFolder(Assembly.GetExecutingAssembly(), "NRails.Tests.Spark.Views");
                             LanguageFactory = NemerleLanguageFactory();
                             ExtensionFactory = NRailsExtensionFactory();
                         };
        }
        
        [Test]
        public MatchTest() : void
        {
            //def a = SparkViewEngine();
            //a.ExtensionFactory

            def engine = MakeEngine();            

            def descriptor = SparkViewDescriptor();
            descriptor.Templates.Add("match.spark");
           
            def entry = engine.CreateEntry(descriptor);
            
            Assert.That(entry, Is.Not.Null);
            
            def view = entry.CreateInstance();
            def result = view.RenderView();
            
            Assert.That(result, Is.EqualTo("\r\ntrue\r\n"))
        }

        [Test]
        public FunTest() : void
        {
            def engine = MakeEngine();
            
            def descriptor = SparkViewDescriptor();
            descriptor.Templates.Add("fun.spark");
           
            def entry = engine.CreateEntry(descriptor);
            
            Assert.That(entry, Is.Not.Null);
            
            //Console.WriteLine(entry.SourceCode);
            
            def view = entry.CreateInstance();
            def result = view.RenderView();
            
            def test = Regex.Replace(result, @"[\r\n\t ]+", " ").Trim();
            
            Assert.That(test, Is.EqualTo("1 2 3 end"))
        }
    }
}
